package com.acme.bankapp.service.bank;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.StringTokenizer;

import com.acme.bankapp.Exception.ClientExistsException;
import com.acme.bankapp.domain.bank.*;

public class BankDataLoaderService {

	// TODO: [VY] filename is hardcoded. 
	//	Something like this, will be better: loader(bank, bc, file)
	public void loader(Bank bank,BankService bc){
		File fpath=new File("C:\\eclipse\\workspace\\lab13\\src\\file.txt");
		String in, name;
		Account ac;
		Gender gender;
		int i=0;
		
		if (!fpath.exists()){
			System.out.println("File not Found");
			System.out.println(fpath.getAbsolutePath());
		}
		try {
			RandomAccessFile file=new RandomAccessFile(fpath, "r");
			in=file.readLine();
			while (in!=null){						
				StringTokenizer st = new StringTokenizer(in, "=;");
				st.nextToken();
				while (st.hasMoreTokens()) {
					if (st.nextToken().equals("c")){
						st.nextToken();
						int bal=Integer.parseInt(st.nextToken());
						st.nextToken();
						int overdr=Integer.parseInt(st.nextToken());
						ac = new CheckingAccount(bal,i,overdr);
					} else {
						st.nextToken();
						int bal=Integer.parseInt(st.nextToken());
						st.nextToken();
						st.nextToken();
						ac = new SavingAccount(bal,i);
					}
					st.nextToken();
					name=st.nextToken();
					st.nextToken();
					if (st.nextToken().equals("f")){
						gender = Gender.FEMALE;
					} else {
						gender = Gender.MALE;
					}
					Client cl = new Client(ac, gender, name, i);
					try {
						bc.addClient(bank, cl);
						bc.saveBank(bank);
					} catch (ClientExistsException e) {
						e.getState();
					}
				}
				in=file.readLine();
				i++;
			}
			file.close();
		} catch (IOException w) {
		}
	}
}
